﻿using SysManage.BLL;
using System;
using System.Data;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.WebPage;

namespace ZHDJ.Web.Portal.PhotoWall
{
    public partial class PhotoWallPage : PageBase
    {
        #region 页面属性
        public string PhotoWallID
        {
            get
            {
                return this.Request["PhotoWallID"] ?? "";
            }
        }

        public DataTable DataSource
        {
            get
            {
                return PartyPhotoWallImage.GetFileData(this.PhotoWallID);
            }
        }

        public string PhotoInfo
        {
            get
            {
                DataTable dt = PartyPhotoWall.QueryPhotoInfo(this.PhotoWallID);
                string info = "";
                if (dt.Rows.Count > 0)
                {
                    info = string.Format(@" <div class='photoDivTitle'><table border='0' cellpadding='10' cellspacing='0'><tr><td>作者：</td><td>{0}</td><td>所属组织：</td><td>{1}</td><td>创建时间：</td><td>{2}</td></tr>
                            <tr><td>来源：</td><td>{3}</td><td>图片分类：</td><td>{4}</td><td></td><td></td></tr>
                        </table></div>"
                        , dt.Rows[0]["UserName"].ToString()
                        , dt.Rows[0]["BranchName"].ToString()
                        , Convert.ToDateTime(dt.Rows[0]["CreateTime"]).ToString("yyyy-MM-dd")
                        , string.IsNullOrEmpty(dt.Rows[0]["SourceId"].ToString()) ? "网站" : dt.Rows[0]["SourceId"].ToString()
                        , string.IsNullOrEmpty(dt.Rows[0]["Category"].ToString()) ? "留影墙" : dt.Rows[0]["Category"].ToString());
                    info += "<center><div id='divdesc' style='height:25px;line-height:25px;text-align:center; width:800px;'>{0}</div></center>";
                }
                return info;
            }
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.DataSource.Rows.Count > 0)
            {
                PartyPhotoWall photo = DB.PartyPhotoWalls.Find(PhotoWallID);
                this.lblTitle.Text = photo.Title;
                this.lbTitle.Text = photo.Title;
            }

            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            //加载评论
            string id = "&$$$@";
            if (!string.IsNullOrEmpty(this.PhotoWallID))
            {
                id = this.PhotoWallID;
            }

            this.rptChat.DataSource = PartyComment.GetCommentData(id);
            this.rptChat.DataBind();
            if (CurrentUser == null)
            {
                this.btnSubmit.Visible = false;
            }
            else
            {
                this.btnSubmit.Visible = true;
            }
        }

        #region 按钮事件
        protected void filePhoto_FileSelected(object sender, EventArgs e)
        {
            var rs = UpFileBusiness.Upload(filePhoto, CurrentUser.LoginID);
            if (rs.IsSuccess)
            {
                hdImageId.Text = rs.FileID;
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtContent.Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "alert-msg", "alert('评论内容不能为空！');", true);
                this.LoadData();
                return;
            }

            PartyComment comment = new PartyComment();
            comment.ID = StringHelper.NewID;
            comment.RelationID = this.PhotoWallID;
            comment.CreateBy = CurrentUser.LoginID;
            comment.Content = this.txtContent.Text.Trim();
            comment.CreateTime = DateTime.Now;
            comment.Module = "LYQ";
            comment.DataCreateType = "1";
            comment.ImageDesc = txtImageDesc.Text.Trim();
            if (!string.IsNullOrEmpty(hdImageId.Text))
            {
                comment.ImageID = hdImageId.Text;
            }

            DB.PartyComments.Add(comment);

            if (DB.SaveChanges() > 0)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "alert-msg", "alert('评论成功');", true);
                this.txtContent.Text = "";
                this.txtImageDesc.Text = "";
                this.LoadData();
            }
            else
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "alert-msg", "alert('评论失败，请稍候再重试！');", true);
                this.LoadData();
            }
        }
        #endregion
    }
}